package com.oig.sys.base.controller;


import com.oig.common.exception.BadRequestException;
import com.oig.common.module.SysPageInfo;
import com.oig.common.module.SysRequest;
import com.oig.common.module.SysResponse;
import com.oig.common.module.ValidListDto;
import com.oig.common.validator.GroupAdd;
import com.oig.common.validator.GroupEdit;
import com.oig.sys.base.dto.CountryGroupDto;
import com.oig.sys.base.dto.CountryGroupMemberDto;
import com.oig.sys.base.service.ITbCountryGroupMemberService;
import com.oig.sys.base.service.ITbCountryGroupService;
import com.oig.sys.base.vo.CountryGroupVo;
import com.oig.sys.log.annotation.SysLog;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Tag(name = "国别组，协定国等")
@RestController
@RequestMapping("/countryGroup")
@Slf4j
public class TbCountryGroupController {

    private final ITbCountryGroupService countryGroupService;
    private final ITbCountryGroupMemberService countryGroupMemberService;

    @Autowired
    public TbCountryGroupController(ITbCountryGroupService countryGroupService,
                                    ITbCountryGroupMemberService countryGroupMemberService){
        this.countryGroupService = countryGroupService;
        this.countryGroupMemberService = countryGroupMemberService;
    }

    @PreAuthorize("@pms.hasPermission('/countryGroup/add')")
    @SysLog(value="添加国际组织",apiUrl="/countryGroup/add")
    @Operation(summary = "添加国际组织")
    @PostMapping(value = "/add")
    public CountryGroupVo add(@RequestBody @Validated(GroupAdd.class) CountryGroupDto countryGroupDto){
        log.info("add country Group {}",countryGroupDto);
        return this.countryGroupService.addCountryGroup(countryGroupDto);
    }

    @PreAuthorize("@pms.hasPermission('/countryGroup/update')")
    @SysLog(value="修改国际组织",apiUrl="/countryGroup/update")
    @Operation(summary = "修改国际组织")
    @PostMapping(value = "/update")
    public CountryGroupVo update(@RequestBody @Validated(GroupEdit.class) CountryGroupDto countryGroupDto){
        log.info("update country Group{}",countryGroupDto);
        return this.countryGroupService.updateCountryGroup(countryGroupDto);
    }

    @PreAuthorize("@pms.hasPermission('/countryGroup/cancel')")
    @SysLog(value="逻辑删除国际组织",apiUrl="/countryGroup/cancel")
    @Operation(summary = "逻辑删国际组织")
    @PostMapping(value = "/cancel/{id}")
    public SysResponse<Integer> cancel(@PathVariable("id") Integer id){
        log.info("cancel Country Group id={}", id) ;
        this.countryGroupService.cancelCountryGroup(id);
        return SysResponse.ok(id,"逻辑删除成功");
    }


    @Operation(summary = "查看国际组织,查看详情")
    @PostMapping(value = "/info/{groupId}")
    public CountryGroupVo info(@PathVariable("groupId") Integer groupId){
        log.info("info UserVo id={}", groupId) ;
        return this.countryGroupService.getCountryGroup(groupId);
    }

    @Operation(summary = "根据名称查询组织及国家")
    @PostMapping(value = "/findGroupAllByName")
    public CountryGroupVo findGroupAllByName(@RequestParam("groupName") String groupName){
        log.info("find by group name:{}",groupName);
        return this.countryGroupService.findGroupAllByName(groupName);
    }

    @Operation(summary = "获取所有组织，包含组织国家")
    @PostMapping(value = "/findAllGroupInfo")
    public List<CountryGroupVo> findAllGroupInfo(){
        log.info("find all group ");
        return this.countryGroupService.findAllCountryGroup();
    }


    @PreAuthorize("@pms.hasPermission('/countryGroup/addCountry')")
    @SysLog(value="国际组织添加国家",apiUrl="/countryGroup/addCountry")
    @Operation(summary = "国际组织添加国家,返回组织成员记录id 列表")
    @PostMapping(value = "/addCountry")
    public SysResponse<List<Integer>> addCountry(@RequestBody @Validated ValidListDto<CountryGroupMemberDto> groupMemberList){
        log.info("group add country id={}", groupMemberList) ;
        List<Integer> memberIdList = this.countryGroupMemberService.addGroupMember(groupMemberList.getList());
        return SysResponse.ok(memberIdList,"国际组织添加国家成功");
    }


    @PreAuthorize("@pms.hasPermission('/countryGroup/delCountry')")
    @SysLog(value="国际组织删除国家",apiUrl="/countryGroup/delCountry")
    @Operation(summary = "国际组织删除国家")
    @PostMapping(value = "/delCountry")
    public SysResponse<List<Integer>> delCountry(@RequestBody @Validated ValidListDto<Integer> memberIdList){
        log.info("group del country member id={}", memberIdList) ;
        if (memberIdList.getList()==null ||memberIdList.getList().isEmpty()){
            throw new BadRequestException("至少选择一个成员国") ;
        }
        this.countryGroupMemberService.delGroupMember(memberIdList.getList());
        return SysResponse.ok(memberIdList.getList(),"国际组织删除国家成功");
    }

    @Operation(summary = "条件查询国别组")
    @PostMapping(value = "/findCountryGroup")
    public SysPageInfo<CountryGroupVo> findCountryGroup(@RequestBody SysRequest<CountryGroupDto> countryGroupRequest){
        log.info("find CountryGroup {}", countryGroupRequest) ;
        return this.countryGroupService.findCountryGroup(countryGroupRequest);
    }
}
